﻿#region License Information (GPL v2)
/*
    Source Queries - Source Server Queries
    Copyright (C) 2009  Source Queries Developers

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    
    Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion

using System;
using System.Text.RegularExpressions;

namespace SourceQueries
{
    public class LogPacket
    {
        public string Log = string.Empty;
        public DateTime Time;
        public string Nick = string.Empty;
        public string ClientID = string.Empty;
        public string SteamID = string.Empty;
        public string Team = string.Empty;
        public string Action = string.Empty;
        public ChatMessage Chat;

        public LogPacket(string log)
        {
            Log = log;
            try
            {
                Match match = Regex.Match(log, @"^L (\d{2})\/(\d{2})\/(\d{4}) - (\d{2}):(\d{2}):(\d{2}): (.+?)$");
                Time = new DateTime(
                    int.Parse(match.Groups[3].Value), /*Year*/
                    int.Parse(match.Groups[1].Value), /*Month*/
                    int.Parse(match.Groups[2].Value), /*Day*/
                    int.Parse(match.Groups[4].Value), /*Hour*/
                    int.Parse(match.Groups[5].Value), /*Minute*/
                    int.Parse(match.Groups[6].Value)  /*Second*/
                    );
                log = match.Groups[7].Value;

                match = Regex.Match(log, "^\"(.+?)<(\\d+)><((?:STEAM_\\d:\\d:\\d+|Console))><(\\w*)>\" (.+?)$");
                Nick = match.Groups[1].Value;
                ClientID = match.Groups[2].Value;
                SteamID = match.Groups[3].Value;
                Team = match.Groups[4].Value;
                Action = match.Groups[5].Value;

                Chat = IsChatMessage();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

        private ChatMessage IsChatMessage()
        {
            Match match = Regex.Match(Action, "^(say|say_team) \"(.*)\"\\s*$");
            if (match.Success)
            {
                return new ChatMessage(match.Groups[2].Value, match.Groups[1].Value);
            }

            return null;
        }

        public class ChatMessage
        {
            public string Message = string.Empty;
            public string MessageType = string.Empty;

            public ChatMessage(string message, string messageType)
            {
                Message = message;
                MessageType = messageType;
            }
        }
    }
}